Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Diagnostics
Imports System.Xml
Imports C4F.DevKit.Telephony.Properties

Namespace C4F.DevKit.Telephony

	''' <summary>
	''' This class provides logging functionality in xml file.
	''' </summary>
	Public Class TapiCallEventLog
		Inherits TraceListener

		#Region "Globals"

		''' <summary>
		''' Required for holding xmlfile
		''' </summary>
		Private xmlDoc As XmlDocument

		''' <summary>
		''' Points to "TraceOutput" node of log file.
		''' </summary>
		Private parent As XmlNode

		''' <summary>
		''' Holds path of the xmlfile. 
		''' </summary>
		Private xPath As String = Nothing

		#End Region

		#Region "Constructors"

		''' <summary>
		''' Constructor for TapiCallEventLog
		''' </summary>
		''' <param name="xmlFileName">xml log file name</param>
		Public Sub New(ByVal xmlFileName As String)
			xPath = xmlFileName
		End Sub

		#End Region

		#Region "Helper Methods"

		''' <summary>
		''' Opens the xml file and assigns parent by TraceOutput node.
		''' </summary>
		Private Sub OpenXmlTraceFile()
			xmlDoc = New XmlDocument()
			Dim rootNode As XmlNode = Nothing
			If (Not System.IO.File.Exists(xPath)) Then
				rootNode = xmlDoc.CreateNode(XmlNodeType.Element, Resources.LogTraceOutput, String.Empty)
				parent = xmlDoc.AppendChild(rootNode)
			Else
				Try
					xmlDoc.Load(xPath)
					parent = xmlDoc.FirstChild
					If parent.Name <> Resources.LogTraceOutput Then
					Throw New TapiManagerException()
					End If
				Catch exception As Exception
					Throw New TapiManagerException(Resources.LogOpenXmlTraceFileException, exception)
				End Try
			End If
		End Sub

		''' <summary>
		''' Saves the xml log file.
		''' </summary>
		Private Sub SaveXmlTraceFile()
			Me.xmlDoc.Save(xPath)
		End Sub

		#End Region

		#Region "Overrides"

		Protected Overrides Overloads Sub Dispose(ByVal disposing As Boolean)
			MyBase.Dispose(disposing)
			Me.Dispose()
		End Sub

		''' <summary>
		''' Overriden method for Trace.Write(string message)
		''' </summary>
		''' <param name="message">A message tio write.</param>
		Public Overrides Overloads Sub Write(ByVal message As String)
			Try
				Me.OpenXmlTraceFile()
				Dim parentNode As XmlNode = AddNode(parent, Resources.LogCallLog, String.Empty)
				Me.MakeLogOfMessage(parentNode, message)
				Me.SaveXmlTraceFile()
			Catch exception As Exception
				Throw New TapiManagerException(exception.Message, exception)
			End Try
		End Sub

		''' <summary>
		''' Overriden method for Trace.Write(Object obj)
		''' </summary>
		''' <param name="obj">Object whose information to be looged.</param>
		Public Overrides Overloads Sub Write(ByVal obj As Object)
			Try
				Me.OpenXmlTraceFile()
				Dim entry As CallInfo = CType(obj, CallInfo)
				Dim parentNode As XmlNode = AddNode(parent, Resources.LogCallLog, String.Empty)
				Me.MakeLogOfCallInfo(parentNode, entry)
				Me.SaveXmlTraceFile()
			Catch exception As Exception
				Throw New TapiManagerException(exception.Message, exception)
			End Try
		End Sub

		''' <summary>
		''' Overriden method for Trace.Write(Object obj, string category)
		''' </summary>
		''' <param name="obj">Object whose information to be looged.</param>
		''' <param name="message">A category name to write.</param>
		Public Overrides Overloads Sub Write(ByVal obj As Object, ByVal category As String)
			Try
				Me.OpenXmlTraceFile()
				Dim entry As CallInfo = CType(obj, CallInfo)
				Dim parentNode As XmlNode = AddNode(parent, Resources.LogCallLog, String.Empty)
				Me.MakeLogOfCallInfo(parentNode, entry)
				Me.MakeLogOfCategory(parentNode, category)
				Me.SaveXmlTraceFile()
			Catch exception As Exception
				Throw New TapiManagerException(exception.Message, exception)
			End Try
		End Sub

		''' <summary>
		''' Overriden method for Trace.Write(string message, string category)
		''' </summary>
		''' <param name="message">A message to write.</param>
		''' <param name="category">A category name to write.</param>
		Public Overrides Overloads Sub Write(ByVal message As String, ByVal category As String)
			Try
				Me.OpenXmlTraceFile()
				Dim parentNode As XmlNode = AddNode(parent, Resources.LogCallLog, String.Empty)
				Me.MakeLogOfMessage(parentNode, message)
				Me.MakeLogOfCategory(parentNode, category)
				Me.SaveXmlTraceFile()
			Catch exception As Exception
				Throw New TapiManagerException(exception.Message, exception)
			End Try
		End Sub

		''' <summary>
		''' Overriden method for Trace.WriteLine(string message)
		''' </summary>
		''' <param name="message">A message tio write.</param>
		Public Overrides Overloads Sub WriteLine(ByVal message As String)
			Try
				Me.OpenXmlTraceFile()
				Dim parentNode As XmlNode = AddNode(parent, Resources.LogCallLog, String.Empty)
				Me.MakeLogOfMessage(parentNode, message)
				Me.SaveXmlTraceFile()
			Catch exception As Exception
				Throw New TapiManagerException(exception.Message, exception)
			End Try
		End Sub

		''' <summary>
		''' Overriden method for Trace.WriteLine(string message, string category)
		''' </summary>
		''' <param name="message">A message to write.</param>
		''' <param name="category">A category name to write.</param>
		Public Overrides Overloads Sub WriteLine(ByVal message As String, ByVal category As String)
			Try
				Me.OpenXmlTraceFile()
				Dim parentNode As XmlNode = AddNode(parent, Resources.LogCallLog, String.Empty)
				Me.MakeLogOfMessage(parentNode, message)
				Me.MakeLogOfCategory(parentNode, category)
				Me.SaveXmlTraceFile()
			Catch exception As Exception
				Throw New TapiManagerException(exception.Message, exception)
			End Try
		End Sub

		''' <summary>
		''' Overriden method for Trace.WriteLine(Object obj)
		''' </summary>
		''' <param name="obj">Object whose information to be looged.</param>
		Public Overrides Overloads Sub WriteLine(ByVal obj As Object)
			Try
				Me.OpenXmlTraceFile()
				Dim entry As CallInfo = CType(obj, CallInfo)
				Dim parentNode As XmlNode = AddNode(parent, Resources.LogCallLog, String.Empty)
				Me.MakeLogOfCallInfo(parentNode, entry)
				Me.SaveXmlTraceFile()
			Catch exception As Exception
				Throw New TapiManagerException(exception.Message, exception)
			End Try
		End Sub

		''' <summary>
		''' Overriden method for Trace.WriteLine(Object obj, string category)
		''' </summary>
		''' <param name="obj">Object whose information to be looged.</param>
		''' <param name="message">A category name to write.</param>
		Public Overrides Overloads Sub WriteLine(ByVal obj As Object, ByVal category As String)
			Try
				Me.OpenXmlTraceFile()
				Dim entry As CallInfo = CType(obj, CallInfo)
				Dim parentNode As XmlNode = AddNode(parent, Resources.LogCallLog, String.Empty)
				Me.MakeLogOfCallInfo(parentNode, entry)
				Me.MakeLogOfCategory(parentNode, category)
				Me.SaveXmlTraceFile()
			Catch exception As Exception
				Throw New TapiManagerException(exception.Message, exception)
			End Try
		End Sub

		#End Region

		#Region "Logging Methods"

		''' <summary>
		''' Logs the call information into CallEventLog node of xml log file.
		''' </summary>
		''' <param name="parentNode">CallEventLog node</param>
		''' <param name="entry">Call to be logged</param>
		Private Sub MakeLogOfCallInfo(ByVal parentNode As XmlNode, ByVal entry As CallInfo)
			Me.AddNode(parentNode, Resources.LogCallInitiateTime, entry.CallInitiateTime.ToString())
			Me.AddNode(parentNode, Resources.LogStartTime, entry.StartTime.ToString())
			Me.AddNode(parentNode, Resources.LogEndTime, entry.EndTime.ToString())
			Me.AddNode(parentNode, Resources.LogCallerId, entry.CallerId)
			Me.AddNode(parentNode, Resources.LogCalledId, entry.CalledId)
			Me.AddNode(parentNode, Resources.LogCallDirection, entry.CallDirection.ToString())
			Me.AddNode(parentNode, Resources.LogStatus, entry.CallState.ToString())
			Me.AddNode(parentNode, Resources.LogLineName, entry.LineName)
		End Sub

		''' <summary>
		''' Logs the specific message given into CallEventLog node of xml log file.
		''' </summary>
		''' <param name="parentNode">CallEventLog node</param>
		''' <param name="message">Message to be logged</param>
		Private Sub MakeLogOfMessage(ByVal parentNode As XmlNode, ByVal message As String)
			Me.AddNode(parentNode, Resources.LogMessage, message)
		End Sub

		''' <summary>
		''' Logs the specific category given into CallEventLog node of xml log file.
		''' </summary>
		''' <param name="parentNode">CallEventLog node</param>
		''' <param name="category">category to be logged</param>
		Private Sub MakeLogOfCategory(ByVal parentNode As XmlNode, ByVal category As String)
			Me.AddNode(parentNode, Resources.LogCategory, category)
		End Sub

		''' <summary>
		''' Creates and adds the node under parent node provided.
		''' </summary>
		''' <param name="parentNode">Node name under which new node is to be added.</param>
		''' <param name="childNodeName">New node name.</param>
		''' <param name="nodeText">Text for node.</param>
		''' <returns></returns>
		Private Function AddNode(ByVal parentNode As XmlNode, ByVal childNodeName As String, ByVal nodeText As String) As XmlNode
			Dim childNode As XmlNode = parentNode.OwnerDocument.CreateNode(XmlNodeType.Element, childNodeName, String.Empty)
			childNode.InnerText = nodeText
			parentNode.AppendChild(childNode)
			Return childNode
		End Function

		#End Region

	End Class

End Namespace
